﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using Petra.Gis.ImagingServices;
using Petra.Gis.ImagingServices.Tiff;
using System.Windows.Media.Imaging;
using System.IO;

namespace testTiff
{
  class Program
  {
    static void Main(string[] args)
    {

      //Petra.Gis.Imaging.TransformWPF.testBitmaps();
      //Petra.Gis.Imaging.TransformGdiPlus.rotateImage();

      string file = "";
      string path = "C:\\temp\\GRDS_LCR\\mapy\\";

      //file = "bene0094.cit";
      string fullpath = path + file;

      //FileStream stream = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
      //Petra.Gis.ImagingServices.Cit.CitBitmapDecoder cd = new Petra.Gis.ImagingServices.Cit.CitBitmapDecoder(stream, true, BitmapCreateOptions.None, BitmapCacheOption.Default);

      //FileStream outStream = new FileStream(@"c:\temp\new.tif", FileMode.Create);
      //TiffBitmapEncoder encoder = new TiffBitmapEncoder();
      //encoder.Compression = TiffCompressOption.None;
      //encoder.Frames.Add(cd.Frames[0]);
      //encoder.Save(outStream);


      //file = "Bran_2_0.tif";
      //I301.tif
      file = "I300.tif";
      //file = "BILO053.tif";
      fullpath = path + file;
      Petra.Gis.ImagingServices.TiffBitmapDecoder d = new Petra.Gis.ImagingServices.TiffBitmapDecoder(new Uri(fullpath), BitmapCreateOptions.None, BitmapCacheOption.Default);
      //d.LoadBitonalAsPaletted8bpp = false;


      //BitmapSource bs;

      //BitmapSource bs = d.Frames[0].Blocks[291].Source;

      //BitmapBlock blck = d.Frames[0].Blocks[291];


      System.Windows.Media.PixelFormat pf = d.Frames[4].Format;

      System.Windows.Media.Imaging.BitmapPalette bp = d.Frames[4].Palette;

      BitmapFrame f = d.Frames[4];



      //System.Windows.Int32Rect rct = new System.Windows.Int32Rect(8000, 6000, 1000, 1000);
      //System.Windows.Int32Rect rct = new System.Windows.Int32Rect(0, 0, f.PixelWidth, f.PixelHeight);
      System.Windows.Int32Rect rct = new System.Windows.Int32Rect(0, 6, 5, 10);


      // create temporary bitmap to hold selected image data
      Bitmap tmp = new Bitmap(rct.Width, rct.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

      int size;
      System.Drawing.Imaging.BitmapData bd;

      //BitmapBlock b = null;

      bd = tmp.LockBits(new Rectangle(0, 0, rct.Width, rct.Height),
                  System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

      size = bd.Stride * bd.Height;

      f.CopyPixels(rct, bd.Scan0, size, bd.Stride);

      tmp.UnlockBits(bd);


      tmp.Save("c:\\temp\\testcopypixels.png", System.Drawing.Imaging.ImageFormat.Png);




      System.Drawing.Imaging.ColorPalette pal = tmp.Palette;

      pal.Entries[0] = System.Drawing.Color.FromArgb(0,0,0,0);
      pal.Entries[255] = System.Drawing.Color.FromArgb(255, 180, 180, 180);

      tmp.Palette = pal;

      tmp.Save("c:\\temp\\testcopypixels.png", System.Drawing.Imaging.ImageFormat.Png);

      return;


      //BitmapSource bs = d.Frames[0].Blocks[291].Source;

      return;

      BmpBitmapEncoder be = new BmpBitmapEncoder();

      List<BitmapFrame> bfm = new List<BitmapFrame>();
      //bfm.Add(BitmapFrame.Create(t.Frames[0]));
      //be.Frames = bfm;


      //save
      //FileStream f = new FileStream("e:\\rastry\\test1.bmp",FileMode.Create, FileAccess.Write);
      //be.Save(f);

    }
  }
}
